package view.utils.formatador;

import java.sql.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class FormatadorData extends PlainDocument {

	private static int TAMANHO_TEXTO = 10; // para o formato dd/MM/yyyy
	private static String CARACTERES_DATA = "0123456789/";
	private static String FORMATO = "dd/MM/yyyy";
	
	public void setDate(Date date) {  
        String strr = new java.text.SimpleDateFormat(FORMATO).format(date);  
        try {  
            remove(0, getLength());  
            super.insertString(0, strr, null);  
        } catch (BadLocationException ex) {  
            Logger.getLogger(FormatadorData.class.getName()).log(Level.SEVERE, null, ex);  
        }  
    }  

	@Override
	public void insertString(int offs, String str, AttributeSet a)
			throws BadLocationException {

		// Nao mostrar se a string for null.
		if (str == null) {
			return;
		}

		// Verificar o tamanho maximo permitido
		if (getLength() > TAMANHO_TEXTO) {
			return;
		}

		// Somente para validar os caracteres digitados
		for (int i = 0; i < str.length(); i++) {
			if (CARACTERES_DATA.indexOf(String.valueOf(str.charAt(i))) == -1) {
				java.awt.Toolkit.getDefaultToolkit().beep();
				return;
			}
		}

		// Checar a posicao do caracter que separa a data
		if ((offs == 2) || (offs == 5)) {
			if (!str.equals("/")) {
				return;
			}
		}

		super.insertString(offs, str, a);
		// inserir o caracter que separa
		if ((offs == 1) || (offs == 4)) {
			super.insertString(offs + 1, "/", a);
		}

		// If the user has finished entering validate the date entered by him.
		if (offs == TAMANHO_TEXTO - 1) {
			String strr = getText(0, getLength());
			remove(0, getLength());

			super.insertString(0, getCorrectDate(strr), a);
		}

	}
	
	 private String getCorrectDate(String oldDate) {  
	        String newDate = "";  
	        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(FORMATO);  
	        try {  
	            java.util.Date date1 = formatter.parse(oldDate);  
	            newDate = formatter.format(date1);  
	        } catch (java.text.ParseException ex) {  
	            Logger.getLogger(FormatadorData.class.getName()).log(Level.SEVERE, null, ex);  
	            newDate = "";  
	        }  
	        return newDate;  
	    } 
	 
	 public Date getDate() {  
	        Date newDate = null;  
	        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(FORMATO);  
	        try {  
	            newDate = (Date) formatter.parse(this.getText(0, TAMANHO_TEXTO));  
	        } catch (java.text.ParseException ex) {  
	            //Logger.getLogger(DateDocument.class.getName()).log(Level.SEVERE, null, ex);  
	        } catch (BadLocationException ex) {  
	            //Logger.getLogger(DateDocument.class.getName()).log(Level.SEVERE, null, ex);  
	        }  
	        return newDate;  
	    }  
}
